package com.hotelbooking.mc;

import java.util.List;

import com.hotelbooking.db.DbManager;
import com.hotelbooking.hotel.define.Hotel;

public class HotelMcManager {
	protected static final HotelMcManager instance = new HotelMcManager();

	private static final String selectUserName = "SELECT HotelId,Name FROM hotel";

	private static final String keyPrefix = "hn_%s";

	static {
		initName2Id();
	}

	private static void initName2Id() {
		List<Hotel> hotels = DbManager.instance.executeSqlQuery(selectUserName,
				Hotel.class);
		for (Hotel hotel : hotels) {
			MemCachedManager.instance.replace(getMcKey(hotel.getHotelName()),
					hotel.getHotelId());
		}
	}

	private static String getMcKey(String hotelName) {
		return String.format(keyPrefix, hotelName);
	}

	public int getIdByHotelName(String hotelName) {
		Object hotelId = MemCachedManager.instance.get(getMcKey(hotelName));
		if (hotelId == null) {
			return 0;
		}
		return (Integer) hotelId;
	}
}
